package com.hln.jingwushi.ui

import android.content.Intent
import android.os.Bundle
import com.hln.jingwushi.MyBaseActivity
import com.hln.jingwushi.databinding.ActivityHomeBinding
import com.hln.jingwushi.utils.Constant
import com.hln.jingwushi.utils.DataCacheUtil
import com.gdsz.libbasic.utils.assist.getCurrentDataLong
import com.gdsz.libbasic.utils.assist.getWeek
import com.gdsz.libbasic.utils.assist.getYmd
import com.gdsz.libbasic.utils.assist.weekArray
import com.gdsz.libbasic.utils.view.throttleClick


class HomeActivity : MyBaseActivity<ActivityHomeBinding>() {


    override fun ActivityHomeBinding.initBinding(savedInstanceState: Bundle?) {
        val currentDate = getCurrentDataLong()
        binding.tvDate.text = getYmd(currentDate) + "   ${weekArray[getWeek(currentDate)]}"
        binding.tvName.text = DataCacheUtil.getName()
        binding.ivLightControl.throttleClick {
            startActivity(Intent(this@HomeActivity, LightActivity::class.java))
        }
        binding.ivAlarmLightSwitch.throttleClick {
            startActivity(Intent(this@HomeActivity, AlarmSoundActivity::class.java))
        }
        binding.ivEnvironmentDetection.throttleClick {
            startActivity(Intent(this@HomeActivity, EnvironmentalDetectionActivity::class.java))
        }
        binding.ivAccessControlSetting.throttleClick {
            DataCacheUtil.getAccessUrl()?.apply {
                toWebActivity(this, Constant.ACCESS_CONTROL)
            }
        }
        binding.ivVideoMonitoring.throttleClick {
            startActivity(Intent(this@HomeActivity, VideoMonitoringActivity::class.java))
        }
        binding.ivAirConditioning.throttleClick {
            startActivity(Intent(this@HomeActivity, AirConditioningActivity::class.java))
        }
        binding.ivCarLocation.throttleClick {
//            账号：huolingniao
//            密码：123456
            DataCacheUtil.getCarLocationUrl()?.apply {
                toWebActivity(this, Constant.LOCATION_CAR)
            }
        }

        binding.ivAlcoholTesting.throttleClick {
            DataCacheUtil.getAlcoholUrl()?.apply {
                toWebActivity(this, Constant.LOCATION_CAR)
            }
        }
        binding.ivLawEnforcementDataManagement.throttleClick {
            DataCacheUtil.getLawEnforcementUrl()?.apply {
                toWebActivity(this, Constant.LOCATION_CAR)
            }
        }
        binding.ivSystemSetting.throttleClick {
            startActivity(Intent(this@HomeActivity, SystemSettingActivity::class.java))
        }
        binding.ivEquipmentManagement.throttleClick {
            DataCacheUtil.getEquipmentUrl()?.apply {
                toWebActivity(this, Constant.LOCATION_CAR)
            }
        }
        modbusInit()
    }


    companion object {
        private val TAG = HomeActivity::class.java.simpleName
    }

    override fun onResume() {
        super.onResume()
        binding.tvName.text = DataCacheUtil.getName()
        binding.tvTip.text =
            "方舱运行状态正常，紧急联系电话：${DataCacheUtil.getContactTel()}，紧急联系人：${DataCacheUtil.getContactPerson()}"
    }
    fun toWebActivity(url: String, func: String) {
        startActivity(
            Intent(this@HomeActivity, LoginActivity::class.java)
                .putExtra("url", url)
                .putExtra("func", func)
        )
    }

    override fun notifyData(values: ShortArray) {
        runOnUiThread {
            binding.tvTemp.text = "温度 ${values[0]} ℃  湿度 ${values[1]} %"
        }
    }


}